/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.travelFinder.domain.utils;

import java.text.ParseException;

/**
 *
 * @author Kryl
 */
public class Weekdays {

    public static Weekdays parseString(String days) throws ParseException {
        Boolean[] tmp = parseString2(days);
        return new Weekdays(tmp);
        //
    }

    public static Boolean[] parseString2(String days) throws ParseException {
        
        Boolean[] tmp = new Boolean[7];
        String[] dd = days.split("-");
        String dayStart = dd[0];
        String dayEnd = dd[1];
        int i = 0;
        Boolean inside = Boolean.FALSE;
        int ok = 0;
        for (String day : d) {
            if (day.equalsIgnoreCase(dayStart)) {
                ++ok;
                inside = Boolean.TRUE;
            }
            tmp[i++] = inside;
            if (day.equalsIgnoreCase(dayEnd)) {
                ++ok;
                inside = Boolean.FALSE;
            }
        }
        //  if("pt-sb".equalsIgnoreCase(days)) return new Weekdays(new Boolean[]{true,true,true,true,true,false,false});
        //if ("sb-nd".equalsIgnoreCase(days)) {
        if (ok < 2) {
            throw new ParseException("brak danych dla " + days, 0);
        }
        return tmp;
    }
    private static String[] d = new String[]{"pn", "wt", "sr", "cz", "pt", "sb", "nd"};
    Boolean[] days;

    public Weekdays(Boolean[] days) {
        this.days = days.clone();
    }

    /*
     * check if day day - number of the day in the week 0- monday ..
     */
    public Boolean isActive(int day) {
        if (day > 6) {
            day -= 7;
        }
        return days[day];
    }
    
}
